
/**		
 * 
 *	uk.co.lmfm.guppy.controller.CompareRepositoriesCommand
 *	
 *	@version 1.00 | Jan 13, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.controller 
{
	import uk.co.lmfm.guppy.application.enum.ApplicationConstants;
	import uk.co.lmfm.guppy.model.SVNProxy;
	import uk.co.lmfm.guppy.model.SettingsProxy;
	import uk.co.lmfm.guppy.model.WorkspaceProxy;
	import uk.co.lmfm.guppy.model.vo.AppStateVO;
	import uk.co.lmfm.guppy.model.vo.SettingsVO;
	import uk.co.lmfm.guppy.view.UpdateMediator;

	import org.osflash.thunderbolt.Logger;
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;

	/**
	 * CompareRepositoriesCommand
	 */
	public class CompareRepositoriesCommand extends SimpleCommand implements ICommand 
	{
		/**
		 * notification.getName() == ApplicationConstants.GOT_REPOSITORY_LATEST_REVISION_NUMBER
		 * notification.getBody() == Latest repository revision number
		 */

		override public function execute(notification : INotification) : void
		{
			super.execute(notification);
						var guppyProxy : SVNProxy = facade.retrieveProxy(SVNProxy.NAME) as SVNProxy;
			var settingsProxy : SettingsProxy = facade.retrieveProxy(SettingsProxy.NAME) as SettingsProxy;
			var workspaceProxy : WorkspaceProxy = facade.retrieveProxy(WorkspaceProxy.NAME) as WorkspaceProxy;
			var settings : SettingsVO = settingsProxy.readSettings();
			var appState : AppStateVO = settingsProxy.readAppState();
			
			workspaceProxy.svnRevision = guppyProxy.latestRevisionNumber;
			
			if(settingsProxy.validateSettings(settings).valid)
			{
				var lastRevision : int = appState.lastRevision;
				var latestRevision : int = guppyProxy.latestRevisionNumber;
			
				var diff : int = latestRevision - lastRevision; 
				
				Logger.info("CompareRepositoriesCommand: Local: " + lastRevision + ", Remote: " + latestRevision + ", Diff: " + diff);
			
				if(diff > 0)
				{
					Logger.info("SVN Checkout required (" + diff + " missing revisions)");
					
					guppyProxy.getRevisions(lastRevision, latestRevision);
					sendNotification(ApplicationConstants.CHANGE_VIEW, UpdateMediator.NAME);
				}
				else if(diff < 0)
				{
					Logger.info("Repository revision number is less than current revision. Repositories might have changed, re-downloading");
					sendNotification(ApplicationConstants.FORCE_REPOSITORY_CHECKOUT);
				}
				else
				{
					sendNotification(ApplicationConstants.UPDATE_FLEX_SDKS, false);
				}
			}
		}
	}
}
